import axios from 'axios'

axios.interceptors.request.use(
  (config) => {
    // 客户端会话
    if (sessionStorage.getItem('account') === undefined) {
      window.location.href = '/#/?tip=1'
      return false
    }
    // 服务端是否维护
    // 检验方法不拦截
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

axios.interceptors.response.use(
  (response) => {
    if (response.data.status === 'noauth') {
      // Vue.$route.push({name: 'Login'})
      window.location.href = '/#/?tip=1'
      return false
    }
    if (response.data.status === 'maintain') {
      // Vue.$route.push({name: 'Login'})
      window.location.href = '/#/?tip=2'
      return false
    }
    if (response.data.status === 'frozen') {
      // Vue.$route.push({name: 'Login'})
      window.location.href = '/#/?tip=3'
      return false
    }
    // if (response.data.status && response.data.status === 'maintain') {
    //   alert('维护中')
    //   setTimeout(function () {
    //     window.location.href = '/'
    //   }, 0)
    //   return false
    // }
    return response
  },
  (error) => {
    console.log(error)
    return Promise.reject(error)
  }
)

export default axios
